package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.*;
import com.javasm.service.MisMannageService.AuditService;
import com.javasm.service.MisMannageService.impl.AuditServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/audits/*")
public class AuditServlet extends BaseServlet {


    protected void getname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String userIdStr = req.getParameter("userId");

        Integer userId = 0;

        if (userIdStr != null & !"".equals(userIdStr)) {
            userId = Integer.parseInt(userIdStr);
        }

        AuditService auditService = new AuditServiceImpl();
        UserInfo userInfo = auditService.getname(userId);

        ReturnEntity returnEntity = new ReturnEntity();
        if (userInfo != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(userInfo);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }



    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        String prodName = req.getParameter("prodname");
        String auditsStatus = req.getParameter("auditstatus");
        Integer page = 1;
        Integer pageSize = 10;

        if (pageStr != null & !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (pageSizeStr != null & !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        Integer id = 0;
        HttpSession session = req.getSession();
        //String idStr = session.getId();
        com.javasm.entity.ManageSystemEntity.User user = (com.javasm.entity.ManageSystemEntity.User) session.getAttribute("loginUser");
        Integer userId = user.getUserId();
        /*if(idStr!= null & !"".equals(idStr)) {
            id = Integer.parseInt(idStr);
        }*/

        AuditService auditService = new AuditServiceImpl();
        List<ProdInfo> prodInfos = auditService.getProd(page, pageSize, prodName, auditsStatus, userId);
        Integer totalNum = auditService.getTotalnum(prodName, auditsStatus, userId);
        //System.out.println(totalNum);
        PageInfo pageInfo = new PageInfo(page, pageSize, totalNum);

        ReturnEntity returnEntity = new ReturnEntity();
        if (prodInfos != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(prodInfos);
            returnEntity.setPageInfo(pageInfo);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
            returnEntity.setPageInfo(new PageInfo(1,1,0));
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }


    protected void queryByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String prodName = req.getParameter("prodname");

        Integer id = 0;
       HttpSession session = req.getSession();
        //String idStr = session.getId();

        /*if(idStr!= null & !"".equals(idStr)) {
            id = Integer.parseInt(idStr);
        }*/

        AuditService auditService = new AuditServiceImpl();
        ProdInfo prodInfo = auditService.getProdByName(prodName);


        ReturnEntity returnEntity = new ReturnEntity();
        if (prodInfo != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(prodInfo);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }

    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String prodName = req.getParameter("prodname");
        String auditStatus = req.getParameter("auditStatus");

        AuditService auditService = new AuditServiceImpl();
        Integer integer = auditService.editStatus(prodName, auditStatus);


        ReturnEntity returnEntity = new ReturnEntity();
        if (integer > 0) {
            returnEntity.setRetCode(ReturnCode.EDIT_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.EDIT_OK.getMsg());
        } else {
            returnEntity.setRetCode(ReturnCode.EDIT_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.EDIT_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }


}
